import 'package:flutter/material.dart';
import '../../common/ScreenAdaper.dart';
import 'package:dio/dio.dart';
import '../../config/Config.dart';
import '../../widget/TextInput.dart';
import '../../widget/Tool.dart';

class RegisterPage extends StatefulWidget {
  RegisterPage({Key key}) : super(key: key);
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  String _username;
  String _password;
  String _email;

  // 注册
  _registerPost() async{
    var apiUrl = "${Config.domain}api/book/register";
    Response result=await Dio().post(apiUrl,data:{
      "username":_username,
      "password":_password,
      "email":_email
    });
    var list=result.data;
    print(list);
    Tool.ShowToast(list["msg"]);
    if(list['code'] == 1){
      Navigator.pushReplacementNamed(context, "/login");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.close),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        title: Text("注册"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            _logo(),
            SizedBox(height: 30,),
            Container(
              padding: EdgeInsets.all(S.width(20)),
              child: TextInput(
                text: "请输入用户名",
                onChanged: (value){
                  this._username = value;
                },
              ),
            ),
            Container(
              padding: EdgeInsets.all(S.width(20)),
              child: TextInput(
                text: "请输入邮箱地址",
                onChanged: (value){
                  this._email = value;
                },
              ),
            ),
            Container(
              padding: EdgeInsets.all(S.width(20)),
              child: TextInput(
                text: "请输入密码",
                password: true,
                onChanged: (value){
                  this._password = value;
                },
              ),
            ),
            _register(),
            _login(),
          ],
        ),
      ),
    );
  }

  Widget _logo(){
    return Container(
      margin: EdgeInsets.only(top: S.height(100)),
      width: S.getScreenWidth(),
      alignment:Alignment.topCenter,
      child: Image.asset("images/user.png", fit: BoxFit.cover),
    );
  }
  Widget _register(){
    return Container(
      margin: EdgeInsets.all(S.width(40)),
      height: S.height(80),
      width: double.infinity,
      child: RaisedButton(
        child: Text('注册',style:TextStyle(color: Colors.white)),
        color: Colors.blue,
        onPressed: () {
          _registerPost();
        },
      ),
    );
  }
  Widget _login(){
    return Container(
      margin: EdgeInsets.only(left: S.width(40),right: S.width(40)),
      height: S.height(80),
      width: double.infinity,
      child: RaisedButton(
        child: Text('已有账号 --> 登录'),
        color: Colors.white,
        onPressed: () {
          Navigator.pushNamed(context, "/login");
        },
      ),
    );
  }
}
